package com.dy.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.VO.UserVO;
import com.dy.entity.User;
import com.dy.exception.BusinessException;
import com.dy.mapper.UserMapper;
import com.dy.response.ResultCode;
import com.dy.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.utils.JWTUtils;
import com.dy.utils.MD5Utils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author dy
 * @since 2022-03-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> findUserList(Page<User> page, UserVO userVO) {
        List<User> userList = this.baseMapper.findUserList(page, userVO);
        page.setRecords(userList);
        return page;
    }

    /**
     * 导出用户Excel
     *
     * @param response
     * @param users
     * @throws IOException
     */
    @Override
    public void exportUserExcel(HttpServletResponse response, List<User> users) throws IOException {
        //----- 写入excel文件
        String exportFileName = "TbUserInfo.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("用户信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(users);
    }

    /**
     * 假设这个是你的DAO存储。当然还要这个类让spring管理，当然你不用需要存储，也不需要这个类。
     **/
    public void save(List<User> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        boolean b = this.saveBatch(list);
        if (b) {
            System.out.println("成功");
        } else {
            System.out.println("失败");
        }
    }

    @Override
    public String login(String username, String password) {
        //1.定义一个token
        String token = null;
        //2.调用根据用户名查询用户信息
        User user = this.baseMapper.loadUserByUsername(username);
        if (user != null) {
            //3.拿取获取到的用户信息的盐和密码
            String salt = user.getSalt();
            String userPassword = user.getPassword();
            //4.使用MD5Utils类给传过来的密码进行加盐加密
            String sPassword = MD5Utils.md5Encryption(password, salt);
            //5.用加密过后的密码跟数据库里拿取的密码进行对比
            if (userPassword.equals(sPassword)) {
                //6.使用JWTUtils生成一个token
                token = JWTUtils.sign(username, password);
                //7.把token存入到shiro中
            }else{
                throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                        ResultCode.USER_CREDENTIALS_ERROR.getMessage());
            }
        } else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return token;
    }
}
